package com.by.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.cloud.annotation.PreAuthorize;
import com.by.cloud.common.BaseResponse;
import com.by.cloud.common.ResultUtils;
import com.by.cloud.enums.UserRoleEnum;
import com.by.cloud.model.dto.category.CategoryUpdateDto;
import com.by.cloud.model.entity.Category;
import com.by.cloud.model.vo.category.CategoryVo;
import com.by.cloud.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author linweirui
 * @date 2024/12/17 10:08
 */
@RestController
@RequestMapping("/category")
@Api(tags = "分类模块")
@RequiredArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;

    @ApiOperation("查询所有分类")
    @GetMapping("/list")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    public BaseResponse<List<CategoryVo>> getList() {
        List<Category> categoryList = categoryService.list();
        return ResultUtils.success(BeanUtil.copyToList(categoryList, CategoryVo.class));
    }

    @ApiOperation("批量新增分类")
    @PostMapping("/add")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    public BaseResponse<Boolean> addBatchCategories(@RequestParam List<String> categoryNameList) {
        boolean isSuccess = categoryService.addBatchCategories(categoryNameList);
        return ResultUtils.success(isSuccess);
    }

    @ApiOperation("批量删除分类")
    @DeleteMapping("/delete")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    public BaseResponse<Boolean> deleteByCategoryIds(@RequestParam("ids") List<Long> ids) {
        boolean result = categoryService.deleteByCategoryId(ids);
        return ResultUtils.success(result);
    }

    @ApiOperation("编辑修改分类名称")
    @PostMapping("/update")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    public BaseResponse<Boolean> updateCategoryNameById(@RequestBody CategoryUpdateDto categoryUpdateDto) {
        boolean result = categoryService.updateCategoryNameById(categoryUpdateDto);
        return ResultUtils.success(result);
    }
}
